// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
//用于对题目数据库操作方法
const db = uniCloud.database();
const dbCmd = db.command

module.exports = {
	_before: function() { // 通用预处理器

	},
	//获取题库信息
	async getdsData(uid, cid, chapterId) {
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		});

		const dsTemp = dbJQL.collection("ds").where({
			Cid: cid,
			Chapterid: chapterId
		}).getTemp();
		const historyTemp = dbJQL.collection("history_person").where({
			uid: uid,
			// subject_id:cid,
			// Chapterid:chapterId
		}).getTemp();

		const questionData = dbJQL.collection(dsTemp, historyTemp).orderBy("name", "asc").get();
		return questionData;

	},

	//顺序刷题查询
	async getSequestion(uid, cid) {
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		});
		const dsTemp = dbJQL.collection("ds").where({
			Cid: cid,
		}).getTemp();

		const historyTemp = dbJQL.collection("history_person").where({
			uid: uid,
		}).getTemp();
		

		const sequestionData = dbJQL.collection(dsTemp, historyTemp)
			.orderBy("Chapterid", "asc")
			.limit(120)
			.get();

		return sequestionData;
	},
	//随机刷题
	async getRandomData(uid, cid,Chapterid) {
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		});
		
		const dsTemp = dbJQL.collection("ds").where({
			Cid: cid,
			Chapterid: Chapterid
		}).getTemp();

		const historyTemp = dbJQL.collection("history_person").where({
			uid: uid,
		}).getTemp();

		const randomData = dbJQL.collection(dsTemp, historyTemp).get();

		return randomData;
	},

	//按模式获取基础、提高题库 0是基础 1是提高
	async getQuestionModeData(uid, cid, chapterId, mode) {
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		});
		const modeInt = parseInt(mode);
		console.log("mode", modeInt);
		const dsTemp = dbJQL.collection("ds").where({
			Cid: cid,
			Chapterid: chapterId,
			tagDifficulty: modeInt
		}).getTemp();

		const historyTemp = dbJQL.collection("history_person").where({
			uid: uid,
		}).getTemp();
		const questionModeData = dbJQL.collection(dsTemp, historyTemp)
			.orderBy("name", "asc")
			.get();

		return questionModeData;

	},

	// //获取该题对应的学科名称
	async getSubjectName(cid) {
		const getsubjectName = await db.collection("subject_classification")
			.where({
				cid: cid
			}).field("subjectName")
			.get();
		const subjectName = await getsubjectName.result.data[0].subjectName;
		return subjectName;
	},
	//获取该题对应的章节名称
	async getChapterName(cid, Chapterid) {
		const getchapterName = await db.collection("chapter")
			.where({
				cid: cid,
				Chapterid: chapterId
			}).field("ChapterName")
			.get();
		const chapterName = await getchapterName.result.data[0].ChapterName;
		return chapterName;
	},



	//将用户选择的选项添加到用户的历史记录表中
	async updataUserOptionRes(uid, subject_id, subjectName, Chapterid, ChapterName, qid, qOrder, optionRes,
		optionResType) {
		const time = Date.now();
		const updataType = db.collection("history_person").add({
			uid: uid,
			subject_id: subject_id,
			Chapterid: Chapterid,
			qid: qid,
			qOrder: qOrder,
			optionRes: optionRes,
			optionResType: optionResType,
			subjectName: subjectName,
			ChapterName: ChapterName,
			historyCreateTime: time
		});
		return updataType;
	},
	//将用户选择的选项添加到考试记录表中
	async updataExamUserOptionRes(uid, subject_id, subjectName, Chapterid, ChapterName, qid, qOrder, optionRes,
		optionResType) {
		const updataType = db.collection("exam").add({
			uid: uid,
			subject_id: subject_id,
			Chapterid: Chapterid,
			qid: qid,
			qOrder: qOrder,
			optionRes: optionRes,
			optionResType: optionResType,
			subjectName: subjectName,
			ChapterName: ChapterName,
		});
		return updataType;
	},
	//更新刷题数
	async updataUserBrushes(uid) {
		const res = await db.collection("userBrushes").where({
			uid: uid
		}).update({
			brushProgress: dbCmd.inc(1), // 自增1
		})
		return res;
	},
	//更正正确的题数
	async updataUserRight(uid) {
		const res = await db.collection("userBrushes").where({
			uid: uid
		}).update({
			accuracyNum: dbCmd.inc(1) // 自增1
		})
		return res;
	},
	//获取统考组卷题库
	async getExamDsData(cid,uid) {
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		});
		
		const dsTemp = await dbJQL.collection("ds").where({
			Cid: cid,
		}).getTemp();
		
		const examTemp = dbJQL.collection("exam").where({
			uid: uid,
		}).getTemp();
		
		const examData = dbJQL.collection(dsTemp, examTemp)
			.orderBy("Chapterid", "asc")
			.limit(200)
			.get();
			
		return examData;
	
	},
	//更新用户收藏的题目
	async updataCollectData(uid, subject_id, Chapterid,qid,collectType) {
		const res = await db.collection("history_person").where({
			uid: uid,
			subject_id: subject_id,
			Chapterid: Chapterid,
			qid:qid
		}).update({
			collectType:collectType
		})
		return res;
	},

}